package net.service.Impl;

import net.common.ServiceResponse;
import net.mapper.CategoryMapper;
import net.mapper.OrderMapper;
import net.mapper.ProductMapper;
import net.pojo.*;
import net.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    OrderMapper orderMapper;
    @Override
    public List<Product> findAll() {
        return productMapper.findAll();
    }

    @Override
    public List<Product> search(ProductVo productVo) {
        List<Product> list = productMapper.search(productVo);
        for (Product product :list){
            Category category = categoryMapper.findById(product.getCid());
            product.setCategory(category);
        }
        return list;
    }

    @Override

    public ServiceResponse addProduct(Product product, @RequestParam(required = false,value = "prfie") MultipartFile file, HttpServletRequest request) {
        String path = request.getServletContext().getRealPath("/products/");
        if (file!= null){
            File dir = new File(path);
            if (!dir.exists()){
                dir.mkdirs();
            }

            String fileName = UUID.randomUUID().toString() + "-" + file.getName();
            File filePath = new File(dir, fileName);

            try {
                file.transferTo(filePath);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("写入失败");
            }

            product.setImgurl("/products/5/"+fileName);
        }
        product.setId(UUID.randomUUID().toString());
        int addProduct = productMapper.addProduct(product);
        if (addProduct > 0){
            return ServiceResponse.createSuccessAndMessage("添加成功");
        }
        return ServiceResponse.createErrorAndMessage("添加失败");
    }

    @Override
    public Product findById(String id) {
        return productMapper.findById(id);
    }

    @Override
    public int deleteById(String id) {
        return productMapper.deleteById(id);
    }

    @Override
    public ServiceResponse updateProduct(Product product) {
        int updateProduct = productMapper.updateProduct(product);
        if (updateProduct > 0){
            return ServiceResponse.createSuccessAndMessage("修改成功");
        }
        return ServiceResponse.createErrorAndMessage("修改失败");
    }

    @Override
    public List<ProductJo> findByCid(Integer cid) {
        return productMapper.findByCid(cid);
    }

    @Override
    public List<Product> searchProduct(String name) {
        return productMapper.searchProduct(name);
    }
    @Override
    public List<Product> countHotSaleProducts(String day) {
        List<Product> productList = new ArrayList<>();
        List<Order> ordersList = orderMapper.findByDayInInterval(day);
        //获取按销量排序的商品
        List<Orderitems> orderitemList = orderMapper.findItemByOrdersid(ordersList);
        for (Orderitems item : orderitemList) {
            Product product = productMapper.findById(item.getProduct_id());
            productList.add(product);
        }
        if (productList.size() > 0) {
            return productList;
        }
        return null;

    }
}
